local skel = fk.CreateSkill {
  name = "emo__liushuo",
}

Fk:loadTranslationTable{
  ["emo__liushuo"] = "流烁",
  [":emo__liushuo"] = "游戏开始时，你摸一张红色牌记为“荧”，再摸一张黑色牌记为“惑”。这两张牌被弃置时，摸两张牌，使用时无距离限制且可增加或减少一个目标。",

  ["#emo__liushuo-choose"] = "巧说：你可以为%arg增加或减少一个目标",
  ["@@emo__bright"] = "<font color='#ff3300'>荧</font>",
  ["@@emo__confuse"] = "<font color='#888888'>惑</font>",
}

skel:addEffect(fk.GameStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local list = { {Card.Red, "@@emo__bright"}, {Card.Black, "@@emo__confuse"} }
    for _, dat in ipairs(list) do
      if player.dead then return end
      -- 优先找能指定目标的牌
      local pile = table.filter(room.draw_pile, function(id) return Fk:getCardById(id).color == dat[1] end)
      table.shuffle(pile)
      local cid = table.find(pile, function(id)
        local c = Fk:getCardById(id)
        return (c:isCommonTrick() or c.type == Card.TypeBasic) and not c.is_passive
      end) or table.random(pile)
      if cid then
        room:moveCardTo(cid, Player.Hand, player, fk.ReasonPrey, skel.name, nil, true, player, dat[2])
      end
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player.dead then return end
    local choices = {}
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
            if Fk:getCardById(info.cardId):getMark("@@emo__bright") ~= 0 then
              table.insertIfNeed(choices, "bright")
            elseif Fk:getCardById(info.cardId):getMark("@@emo__confuse") ~= 0 then
              table.insertIfNeed(choices, "confuse")
            end
          end
        end
      end
    end
    if #choices > 0 then
      event:setCostData(self, #choices)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    for _ = 1, event:getCostData(self) do
      if player.dead then return end
      player:drawCards(2, skel.name)
    end
  end,
})

skel:addEffect(fk.AfterCardTargetDeclared, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.tos and
      (data.card:getMark("@@emo__bright") ~= 0 or data.card:getMark("@@emo__confuse") ~= 0) and
      (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = data:getExtraTargets({bypass_distances = true})
    if #data.tos > 1 then -- 大于1个目标时才能取消目标
      table.insertTable(targets, data.tos)
    end
    if #targets == 0 then return false end
    local to = room:askToChoosePlayers(player, {
      skill_name = skel.name,
      min_num = 1,
      max_num = 1,
      targets = targets,
      prompt = "#emo__liushuo-choose:::"..data.card:toLogString(),
      cancelable = true,
      extra_data = table.map(data.tos, Util.IdMapper),
      target_tip_name = "addandcanceltarget_tip",
    })
    if #to > 0 then
      to = to[1]
      if table.contains(data.tos, to) then
        data:removeTarget(to)
      else
        data:addTarget(to)
      end
    end
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and (card:getMark("@@emo__bright") ~= 0 or card:getMark("@@emo__confuse") ~= 0)
  end,
})

return skel
